package component.fy.com.common_module.services;

import android.app.DownloadManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;

import java.io.File;

import component.fy.com.common_module.base.BaseCommonApp;
import component.fy.com.common_module.constant.AppConstant;
import component.fy.com.common_module.utils.LogUtil;

/**
 * Created by Administrator on 2017/10/26 0026.
 */

public class UpdateService extends Service {
    private static final String TAG = "UpdateService";
    //记得先申请权限 WRITE_EXTERNAL_STORAGE
    /**
     * 广播接受者
     */
    private BroadcastReceiver receiver;
    /**
     * 系统下载管理器
     */
    private DownloadManager dm;
    /**
     * 系统下载器分配的唯一下载任务id，可以通过这个id查询或者处理下载任务
     */
    private long enqueue;
    /**
     * TODO下载地址 需要自己修改,这里随便找了一个
     */
    private String downloadUrl = AppConstant.apk_download_url;//AppConstant.apk_download_url

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //Apk文件路径可自定义，此处用 download 文件夹
        File apkfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(), AppConstant.apk_file_name);
        if (!apkfile.exists()) {
            LogUtil.e(TAG, "安装包不存在");
            receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    install(context);
                    //销毁当前的Service
                    stopSelf();
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            };
            registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
            //判断网络
        /*if (TextUtils.equals(MyUtil.getCurrentNetType(this), "wifi")) {
            startDownload(downloadUrl);
        } else {
            ToastUtil.showToast("请连接无线网络再尝试更新！");
            stopSelf();
        }*/
            startDownload(downloadUrl);
        } else {
            LogUtil.e(TAG, "安装包存在");
            install(getApplicationContext());
            //销毁当前的Service
            stopSelf();
//            android.os.Process.killProcess(android.os.Process.myPid());
            BaseCommonApp.getBaseApp().getActivityManager().finishAllActivity();
        }
        return Service.START_STICKY;
    }

    /**
     * 通过隐式意图调用系统安装程序安装APK
     */
    public static void install(Context context) {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), AppConstant.apk_file_name);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // 由于没有在Activity环境下启动Activity,设置下面的标签
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 24) { //判读版本是否在7.0以上
            //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致   参数3  共享的文件
            Uri apkUri = FileProvider.getUriForFile(context, "com.mycomponent.fileprovider", file);
            //添加这一句表示对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }

    @Override
    public void onDestroy() {
        //服务销毁的时候 反注册广播
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
        super.onDestroy();
    }

    private void startDownload(String downUrl) {
        //获得系统下载器
        dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        //设置下载地址
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downUrl));
        //设置下载文件的类型
        request.setMimeType("application/vnd.android.package-archive");
        //设置下载存放的文件夹和文件名字
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, AppConstant.apk_file_name);
        //设置下载时或者下载完成时，通知栏是否显示
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setTitle("饭碗易购app更新下载...");
        //执行下载，并返回任务唯一id
        enqueue = dm.enqueue(request);
    }
}
